'''

sprtid: N1
schedule: 3
lang: 2
spread: c
odd: hk
evntid: 4190872
date: 0
plytypgrp: 1
csrf_name: csrf64ba5ecc5a797
csrf_value: 1f9a0377f7d98c7932645f313436522e

evntid: 4184113
evntid: 4189557

OD 1 8:让球
OD 1 9;大小

'''
import time

import requests

from sql.sql_conn import Mysql_data
from sql.sql_state import insert_data


class Ss(Mysql_data):
    def ss_comm(self):

        t2 = time.time()
        headers1 = {
            # "Content-Type": "application/json;charset=utf-8",
            #
            #     'User-Agent': random.choice(USER_AGENT_LIST)
            'Accept': '*/*',
            'Accept-Encoding': 'gzip, deflate, br',
            'Accept-Language': 'zh,zh-CN;q=0.9',
            'Cookie': 'WEBSESSID=rdu68qkmgihiek2uaaricr8sm0; p0001_lang=zh_CN; p0001_token=dW5kZWZpbmVk; p0001_acc=%7B%22previewFlag%22%3Atrue%2C%22isDemo%22%3Afalse%2C%22spread%22%3A%22c%22%2C%22oddsType%22%3A%22hk%22%2C%22showDemo%22%3Afalse%2C%22showLogo%22%3Atrue%2C%22selectedMarket%22%3Anull%7D',
            'Referer': 'https://dywu.sp5178.com/whg/preview?langs=2',
            'Sec-Ch-Ua': '"Not.A/Brand";v="8", "Chromium";v="114", "Google Chrome";v="114"',
            'Sec-Ch-Ua-Mobile': '?0',
            'Sec-Ch-Ua-Platform': '"Windows"',
            'Sec-Fetch-Dest': 'empty',
            'Sec-Fetch-Mode': 'cors',
            'Sec-Fetch-Site': 'same-origin',
            'Token': 'QVI=',
            'User-Agent': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/114.0.0.0 Safari/537.36',
            'X-Requested-With': 'XMLHttpRequest',
        }
        url = "https://dywu.sp5178.com/whg/sport-odds-preview/3?sprtid=N1&lang=2&spread=c&odd=hk&page=1&sorttyp=1&date=0&plytypgrp=1&gameMenuId=0"
        req = requests.get(url, headers=headers1, timeout=2)
        try:
            req_data = req.json()
            # print(req_data)
            req.close()
            return req_data
        except Exception as e:
            # print(req.text)
            print(e)
        # finally:
        #     print(time.time()-t2)

    def ss_handle(self, t1, t2):
        data = self.ss_comm()
        events = data['events']
        data_list_quan_daxiao = []
        data_list_ban_daxiao = []
        data_list_quan_ran = []
        data_list_ban_ran = []
        for eve in events:
            for eve_data in eve:
                eveid = eve_data['eveid']  # 比赛ID

                # dt = eve_data['dt']  # 开赛时间
                lgecapt = eve_data['lgecapt']  # 联赛名称
                tacapt = eve_data['tacapt']  # 主队
                tbcapt = eve_data['tbcapt']  # 客队
                evetm = eve_data["rbinfo"]["evetm"]  # 时间
                rbinfo_scoa_scob = str(eve_data["rbinfo"]["scoa"]) + ":" + str(eve_data["rbinfo"]["scob"])  # 得分

                mm = 0
                if "角球" in tbcapt:
                    '科隆'
                    lgecapt = lgecapt[:-7]
                    tacapt = tacapt[:-4]
                    tbcapt = tbcapt[:-4]
                    mm = 2

                if eve_data['od'] != []:
                    od_1 = eve_data['od']['1']

                    od_data = self.od(od_1, tacapt, tbcapt, mm)
                    if od_data != []:
                        for i in od_data:
                            data_all = t1, lgecapt, evetm, rbinfo_scoa_scob, tacapt, tbcapt, i[0], i[1], i[2], i[3], i[
                                4], i[5], i[6], t2
                            # print(data_all)
                            if '让' in i[0]:
                                data_list_quan_ran.append(data_all)
                            else:
                                data_list_quan_daxiao.append(data_all)

                    try:
                        od_2 = eve_data['od']['2']
                        mm = mm + 1
                        od_data_ban = self.od(od_2, tacapt, tbcapt, mm)
                        if od_data_ban != []:
                            for i in od_data_ban:
                                data_all = t1, lgecapt, evetm, rbinfo_scoa_scob, tacapt, tbcapt, i[0], i[1], i[2], i[3], \
                                i[4], \
                                    i[5], i[6], t2
                                # print(data_all)
                                if '让' in i[0]:
                                    data_list_ban_ran.append(data_all)
                                else:
                                    data_list_ban_daxiao.append(data_all)

                    except Exception as e:
                        # print("od_2 = eve_data['od']['2']", e)
                        pass
        t5 = time.time()
        if len(data_list_quan_daxiao) != 0:
            idd = insert_data("ss")
            self.run_sql(idd, data_list_quan_daxiao)
            # print(idd)
        if data_list_ban_daxiao != []:
            idd2 = insert_data("ss_ban")
            self.run_sql(idd2, data_list_ban_daxiao)
            # print(idd2)
        if len(data_list_quan_ran) != 0:
            idf3 = insert_data("ss_ran")
            self.run_sql(idf3, data_list_quan_ran)
            # print(idf3)
        if data_list_ban_ran != []:
            idd4 = insert_data("ss_ban_ran")
            self.run_sql(idd4, data_list_ban_ran)
            # print(idd4)
        print("***",time.time()-t5)
    def od(self, od_data, zhu, ke, n):
        ran_all = []
        if n == 0:
            ran_name = '全场让'
            da_name = '全场大小'
        elif n == 1:
            ran_name = '上半场让'
            da_name = '上半场大小'
        elif n == 2:
            ran_name = '全场角让'
            da_name = '全场角球大小'
        elif n == 3:
            ran_name = '上半场角让'
            da_name = '上半角球大小'
        try:
            od_ran = od_data['8']
            for ran in od_ran:
                hdp = od_ran[ran]["hdp"]  # 盘口
                fav = od_ran[ran]["fav"]  # 定 + -
                oddsa = od_ran[ran]["oddsa"]
                oddsb = od_ran[ran]["oddsb"]
                if fav == 'a':
                    ran_od = ran_name, zhu, '-' + hdp, oddsa, ke, hdp, oddsb
                else:
                    ran_od = ran_name, zhu, hdp, oddsa, ke, '-' + hdp, oddsb
                ran_all.append(ran_od)
        except Exception as e:
            # print('od_ran = od_data[8]', e)
            pass
        try:
            od_da = od_data['9']
            for da in od_da:
                hdp = od_da[da]["hdp"]  # 盘口
                oddsa = od_da[da]["oddsa"]
                oddsb = od_da[da]["oddsb"]
                ran_od = da_name, '大', hdp, oddsa, '小', hdp, oddsb
                ran_all.append(ran_od)
        except Exception as e:
            # print('od_ran = od_data[9]', e)
            pass
        return ran_all


#
#
if __name__ == '__main__':
    while True:

        aa = Ss()
        t1 = time.strftime("%Y-%m-%d %X", time.localtime())
        t2 = time.time()
        aa.ss_handle(t1, t2+15)
        print(time.time()-t2)
        time.sleep(1)

